<?php
namespace app\admin\controller;
class Carousel extends Common{
    function index($module_id=0){
        $artmodules=model('artmodule')->readAll();
        $this->assign('artmodules',$artmodules);
        $map=[];
        if($module_id>0){
            $map=['module_id'=>$module_id];
        }
        $list=model($this->table)->readAll($map,true,['with'=>'module','order'=>'sort,id']);
        $this->assign('list',$list);
        $this->assign('module_id',$module_id);
        return view();
    }
    function add(){
        $id=input('id/d',0);
        $file=request()->file('file');
        if($file){
            $image=input('image/s','');
            $f=$this->loadHeadImg($file);
            if(!$f['success']){
                return json($f['msg'],1);
            }
            if($image){
                $this->deleteUploadFile($image);
            }
            if($id>0){
                $edit=model($this->table)->edit(['id'=>$id,'image'=>$f['msg']]);
                if($edit){
                    return json('更改图片成功',0,$f['msg']);
                }
            }

            return json('',0,$f['msg']);
        }
        if(request()->isAjax()||request()->isPost()){
            $data=[
                'title'=>input('title/s'),
                'module_id'=>input('module_id/d'),
                'link'=>input('link/s',''),
                'status'=>input('status/d',1),
                'is_blank'=>input('is_blank/d',0),
                'image'=>input('image/s'),
            ];
            $validate=$this->validate($data,[
                'title|标题'=>'require',
                'module_id|模块'=>'require',
                'image|轮播图'=>'require',
            ]);
            if(true!==$validate){
                $this->error($validate);
            }
            if($id>0){
                $has=model($this->table)->read(['id'=>$id],'image');
                if(!empty($has)){
                    $data['id']=$id;
                    model($this->table)->edit($data);
                    $this->success('编辑成功');die;
                }
            }
            $data['sort']=time();
            $add=model($this->table)->add($data);
            if(!$add){
                $this->error('系统繁忙，新增失败');
            }
            $this->success('新增成功');
        }
    }
    /**
     * 启用、隐藏
     * @param  integer $id 用户ID
     * @return [type]      [description]
     */
    function close($id=0){
        if(request()->isAjax()||request()->isPost()){
            $this->superCheck();
            $ids=input('ids/a',[]); // 多项
            if($id<=0&&empty($ids)){
                $this->error(lang('Data does not exist'));
            }
            $where="";
            if(!empty($ids)){
                $ids=implode(',', $ids);
                $where=" `id` in ({$ids}) ";
            }else{
                $where=" `id`={$id} ";
            }
            $rs=model($this->table)->close($where,'status',true);
            if($rs){
                $this->success(lang('Successful operation'));
                die;
            }
            $this->error(lang('Failed operation'));
        }
    }
    /**
     * 删除
     * @param  integer $id 用户ID
     * @return [type]      [description]
     */
    function del($id=0){
        if(request()->isAjax()||request()->isPost()){
            if($id<=0){
                $this->error(lang('Data does not exist'));
            }
            $map=[
                'id'=>$id,
            ];
            $has=model($this->table)->read($map,'image');
            if(empty($has)){
                $this->success(lang('Successful operation'));
                die;
            }
            $rs=model($this->table)->del($map);
            if($rs){
                $this->deleteUploadFile($has['image']);
                $this->success(lang('Successful operation'));
                die;
            }
            $this->error(lang('Failed operation'));
        }
    }
}